package org.springblade.pm.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.tool.api.R;
import org.springblade.pm.entity.Active;
import org.springblade.pm.entity.ClientSatisfaction;
import org.springblade.pm.service.IActiveService;
import org.springblade.pm.service.IClientSatisfactionService;
import org.springblade.pm.service.IXmglProjectService;
import org.springblade.pm.vo.ClientSatisfactionVo;
import org.springblade.pm.wrappper.ClientSatisfactionWrapper;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * 项目管理活动
 */
@RestController
@RequestMapping("quality")
@AllArgsConstructor
public class QualityController {

	private IClientSatisfactionService clientSatisfactionService;
	private IActiveService activeService;
	private IXmglProjectService xmglProjectService;

	/**
	 * 新增或修改客户满意度
	 * @param clientSatisfaction
	 * @param bladeUser
	 * @return
	 */
	@PostMapping("/saveClientSatisfaction")
	@ApiOperation(value = "新增修改客户满意度")
	public R saveClientSatisfaction(ClientSatisfaction clientSatisfaction, BladeUser bladeUser){
		Integer id = clientSatisfaction.getId();
		if(id==null||"".equals(id)){
			clientSatisfaction.setCreateTime(new Date());
			clientSatisfaction.setCreateUser(bladeUser.getUserId());
		}
		return R.status(clientSatisfactionService.saveOrUpdate(clientSatisfaction));
	}

	/**
	 * 获取活动各种情况下的数量
	 * @param projectId
	 * @return
	 */
	@GetMapping("/getActiveData")
	@ApiOperation(value = "获取活动的各种数据")
	public R getActiveData(Integer projectId){
		HashMap<String, Integer> activeMap = new HashMap<>();
		Active active = new Active();
		active.setProjectId(projectId);


		Integer activeSum = activeService.count(Condition.getQueryWrapper(active));
		activeMap.put("activeSum",activeSum);
		active.setActiveConclusion("符合");
		Integer conformCount = activeService.count(Condition.getQueryWrapper(active));
		activeMap.put("conformCount",conformCount);
		active.setActiveConclusion("不符合");
		Integer noconformCount = activeService.count(Condition.getQueryWrapper(active));
		activeMap.put("noconformCount",noconformCount);
		return R.data(activeMap);
	}

	/**
	 * 获取这个项目的bug总数和各级严重程度的bug数量
	 * @param projectCode
	 * @return
	 */
	@GetMapping("/getBugData")
	@ApiOperation(value = "获取活动的各种数据")
	public R getBugData(String projectCode){
		return R.data(xmglProjectService.getBugList(projectCode));
	}

	/**
	 * 客户满意度列表
	 *
	 * @param projectId
	 * @return
	 */
	@GetMapping("/clientSatisfactionList")
	@ApiOperation(value = "客户满意度列表")
	public R clientSatisfactionList(Integer projectId){
		QueryWrapper<ClientSatisfaction> wrapper = new QueryWrapper<>();
		wrapper.eq("project_id",projectId);
		return R.data(ClientSatisfactionWrapper.build().entityVoList(clientSatisfactionService.list(wrapper)));
	}
}
